import logging
import unittest

from api.user_rights_api import UserRightsApi
from common.db_util import DBUtil
import config.db_cnf as db_cnf
import config.params_cnf as params_cnf


class TestUserOrder(unittest.TestCase):
    dbUtil = None
    order_id = None
    order_no = None

    @classmethod
    def setUp(cls) -> None:
        cls.dbUtil = DBUtil(host=db_cnf.host, port=db_cnf.port,
                            user=db_cnf.user, password=db_cnf.password,
                            database=db_cnf.database)

    def test01_get_token(self):
        print('获取token断言')

        resp = UserRightsApi.get_token()
        self.assertEqual(200, resp.status_code)

        params_cnf.TOKEN_BODY['token'] = resp.json().get('token')
        logging.info(params_cnf.TOKEN_BODY)

        self.assertEqual(32, len(resp.json().get('token')))

    def test02_verify_token(self):
        print('Token验证')

        resp = UserRightsApi.verify_token()
        self.assertEqual(200, resp.status_code)
        self.assertTrue(resp.json().get('isValid'))

    def test03_get_address(self):
        print('获取地址信息')

        resp = UserRightsApi.get_address()
        self.assertEqual(200, resp.status_code)

        # 预期结果：数据库user_address中当前用户数据
        sql = f"SELECT * FROM user_address WHERE user_id = '8'"
        db_data_name = self.dbUtil.dql_one(sql)[1]
        logging.info(db_data_name)

        resp_name = resp.json().get('name')
        self.assertEqual(db_data_name, resp_name)
